import 'dart:async';
import 'package:qgchat/bloc/blocProvider.dart';
import 'package:qgchat/common/global/global.dart';
import 'package:qgchat/http/apis/user.dart';
import 'package:qgchat/http/apis/video.dart';
import 'package:qgchat/http/models/anchorModel.dart';
import 'package:qgchat/http/models/innerpageModel.dart';
import 'package:video_player/video_player.dart';

class InnerBloc implements BlocBase {
  // 视频控制器
  VideoPlayerController videoPlayerController1;
  // 异步加载小视频
  Future initializeVideoPlayerFuture;

  /// 小视频信息
  InnerpageModel videoInfo;

  // ##########  STREAMS  ##############

  // 主播详情
  StreamController tvideoInfo = StreamController<InnerpageModel>.broadcast();
  Sink get vdeioInfoSink => tvideoInfo.sink;
  Stream get videoInfoStream => tvideoInfo.stream;
  // 点击喜欢当前小视频
  StreamController clicklovevideo = StreamController<int>.broadcast();
  Sink get cloveSink => clicklovevideo.sink;
  Stream get cloveStream => clicklovevideo.stream;
  // 关注当前主播
  StreamController focuscurrent = StreamController<int>.broadcast();
  Sink get focusSink => focuscurrent.sink;
  Stream get focusStream => focuscurrent.stream;
  // 是否播放改视频
  StreamController isplayvideo = StreamController<bool>.broadcast();
  Sink get isplayvideoSink => isplayvideo.sink;
  Stream get isplayvideoSteam => isplayvideo.stream;
  // 视频土豪榜
  StreamController localtycoon = StreamController<List<Closeman>>.broadcast();
  Sink get localtycoonSink => localtycoon.sink;
  Stream get localtycoonStream => localtycoon.stream;
  // 用户是否购买小视频
  StreamController isbuy = StreamController<List<Closeman>>.broadcast();
  Sink get isbuySink => isbuy.sink;
  Stream get isbuyStream => isbuy.stream;
  // 用户是否加载小视频
  StreamController isonload = StreamController<bool>.broadcast();
  Sink get isonloadSink => isonload.sink;
  Stream get isonloadStream => isonload.stream;

  void dispose() {
    tvideoInfo.close();
    clicklovevideo.close();
    focuscurrent.close();
    isplayvideo.close();
    localtycoon.close();
    isbuy.close();
    isonload.close();
    videoPlayerController1.dispose();
  }

  InnerBloc(int id) {
    videocurrent(id).then((res) {
      getlocaltycoon();
      videoPlayerController1.addListener(() {
        isplayvideoSink.add(videoPlayerController1.value.isPlaying);
        if (!videoPlayerController1.value.initialized) {
          videoPlayerController1.initialize();
        }
      });
    });
  }

  // ##########  HANDLING  ##############

  // 获取当前小视频信息
  Future videocurrent(cid) async {
    InnerpageModel result = await VideoApis.getvideoinfo(cid);
    videoInfo = result;
    vdeioInfoSink.add(result);
    videoPlayerController1 = VideoPlayerController.network(videoInfo.video);
    videoPlayerController1.initialize().then((res) {
      isonloadSink.add(videoPlayerController1.value.initialized);
    });
  }

  // 喜欢当前小视频
  void lovecurrent() async {
    Map isFollow = await VideoApis.clicklove(videoInfo.id);
    if (isFollow != null && isFollow['status'] == 1) {
      cloveSink.add(1);
    }
  }

  // 不喜欢当前小视频
  void cancelcurrent() async {
    Map isHate = await VideoApis.canclelove(videoInfo.id);
    if (isHate != null && isHate['status'] == 1) {
      cloveSink.add(0);
    }
  }

  // 关注当前小视频的主播
  void focusAnchor() async {
    Map isFollow = await UserApis.follow(videoInfo.zhuboId);
    if (isFollow != null && isFollow['status'] == 1) {
      showToast('关注成功');
      focusSink.add(1);
    }
  }

  // 本周土豪榜
  void getlocaltycoon() async {
    List<Closeman> result = await VideoApis.getvideotycool(videoInfo.id);
    localtycoonSink.add(result);
    cloveSink.add(videoInfo.vediostatus);
    focusSink.add(videoInfo.likestatus);
  }

  // 取消关注当前小视频的主播
  void cancelAnchor() async {
    Map isCancelfollow = await UserApis.cancelfollow(videoInfo.zhuboId);
    if (isCancelfollow != null && isCancelfollow['status'] == 1) {
      showToast('取消关注');
      focusSink.add(0);
    }
  }

  // 购买当前小视频
  buyvideo(token) async {
    Map isCancelfollow = await UserApis.buyvideo(videoInfo.id, token);
    if (isCancelfollow != null && isCancelfollow['status'] == 1) {
      showToast('购买成功');
    }
    return isCancelfollow;
  }
}
